package org.esisar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.esisar.model.Address;
import org.esisar.model.Model;

public class AddressStatementCreator implements StatementCreator{
private PreparedStatement preparedStatement;
	
	@Override
	public PreparedStatement addStatement(Connection connection, Model model) throws SQLException {
		
		Address address = modelToAddress(model);
			
		preparedStatement = connection.prepareStatement("INSERT INTO address (idAddress,numero,street,postCode,city,country,idClient) VALUES(address_sequence.nextval,?,?,?,?,?,?)");

		fillModel(address);
	
		return preparedStatement;
		
	}

	@Override
	public PreparedStatement deleteStatement(Connection connection, List<Long> keys) throws SQLException {

		preparedStatement = connection.prepareStatement("DELETE FROM address WHERE idAddress=?");

		preparedStatement.setLong(1, keys.get(0));
				
		return preparedStatement;
	}

	@Override
	public PreparedStatement updateStatement(Connection connection, Model model)
			throws SQLException {
		
		Address address = modelToAddress(model);
		
		preparedStatement = connection.prepareStatement("UPDATE address SET numero = ?, street = ?, postCode = ?, city = ?, country = ?, idClient = ? WHERE idAddress = ?");

		fillModel(address);
		preparedStatement.setLong(7, address.getIdAddress());
		
		return preparedStatement;
	}

	private void fillModel(Address address) throws SQLException {
		preparedStatement.setString(1, address.getNumero());
		preparedStatement.setString(2, address.getStreet());
		preparedStatement.setString(3, address.getPostCode());
		preparedStatement.setString(4, address.getCity());
		preparedStatement.setString(5, address.getCountry());
		preparedStatement.setLong(6, address.getIdClient());
	}

	private Address modelToAddress(Model model) {
		Address address;
		
		if (model instanceof Address){
			address = (Address) model;
		}else{
			throw new ClassCastException("Bad object instance");
		}
		return address;
	}

	@Override
	public PreparedStatement readStatement(Connection connection, String field,
			String value) throws SQLException {
		
		preparedStatement = connection.prepareStatement("select * from address where " + field + " = ? ");

		preparedStatement.setString(1, value);
		
		return preparedStatement;
	}

	@Override
	public Model fillModel(ResultSet rs) throws SQLException {
		Long idAddress  = Long.decode(rs.getString("idAddress"));
		String numero = rs.getString("numero");
		String street = rs.getString("street");
		String postCode = rs.getString("postCode");
				
		String city = rs.getString("city");
		String country = rs.getString("country");
		String idClient = rs.getString("idClient");
		
		Address address = new Address(numero, street, postCode, city, country, Long.parseLong(idClient));
		address.setIdAddress(idAddress);
		return address;
		
		
		
	}
}
